﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OurFAS.Data;

namespace OurFAS.MyAssetManage
{
    public partial class FrmAssetDetails : Form
    {
        private EnumHelper.EnumObjctOperateType m_opType;
        private string m_sCode = null;
        private string m_sDataSource = null;

        public FrmAssetDetails(EnumHelper.EnumObjctOperateType opType, string sCode, string sDataSource)
        {
            InitializeComponent();
            m_opType = opType;
            m_sCode = sCode;
            m_sDataSource = sDataSource;

            InitControl(m_opType, sCode, sDataSource);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (m_opType == EnumHelper.EnumObjctOperateType.View)
            {
                this.Close();
            }
            else
            {
                if (MessageBox.Show("您确定要放弃当前操作吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    this.Close();
                }
                else
                {
                    return;
                }
            }
        }

        private void btnOperation_Click(object sender, EventArgs e)
        {
            if (m_opType == EnumHelper.EnumObjctOperateType.New )
            {
                if (MessageBox.Show("您确定要新增当前设备吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    AddAsset();
                }
                else
                {
                    return;
                }
            }
            else if (m_opType == EnumHelper.EnumObjctOperateType.Edit)
            {
                if (MessageBox.Show("您确定要修改当前设备吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    EditAsset(m_sCode);
                }
                else
                {
                    return;
                }
            }
            else if (m_opType == EnumHelper.EnumObjctOperateType.Delete)
            {
                if (MessageBox.Show("您确定要删除当前设备吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    DeleteAsset(m_sCode);
                }
                else
                {
                    return;
                }
            }
        }

        private void btnSync_Click(object sender, EventArgs e)
        {
            InitFrmByDetails(m_sCode,m_sDataSource);
        }

        #region 局部方法
        //初始化控件
        private void InitControl(OurFAS.Data.EnumHelper.EnumObjctOperateType opType, string code, string datasource)
        {
            InitComboBox();

            if (opType ==  EnumHelper.EnumObjctOperateType.View)
            {
                tbAssetCode.ReadOnly = true;
                btnOperation.Visible = false;
                btnSync.Visible = true;
                this.Text = "资产详情（查看）";
            }
            else if (opType == EnumHelper.EnumObjctOperateType.Edit)
            {
                tbAssetCode.ReadOnly = true;
                btnOperation.Visible = true;
                btnSync.Visible = true;
                btnOperation.Text = "编  辑";
                this.Text = "资产详情（编辑）";
            }
            else if (opType == EnumHelper.EnumObjctOperateType.New)
            {
                tbAssetCode.ReadOnly = false;
                btnOperation.Visible = true;
                btnSync.Visible = false;
                btnOperation.Text = "新  增";
                this.Text = "资产详情（新增）";
            }
            else if (opType ==  EnumHelper.EnumObjctOperateType.Delete)
            {
                tbAssetCode.ReadOnly = true;
                btnOperation.Visible = true;
                btnSync.Visible = true;
                btnOperation.Text = "删  除";
                this.Text = "资产详情（删除）";
            }
            InitFrmByDetails(code, datasource);
        }

        //初始化下拉框控件
        private void InitComboBox()
        {
            cmbAssetName.Items.Clear();
            cmbDept.Items.Clear();
            cmbGetType.Items.Clear();
            cmbMeasureInfo.Items.Clear();
            cmbUseFor.Items.Clear();

            //初始化“资产名称”下拉框
            foreach (Dict_Category record in AssetsHelper.GetCategorys())
            {
                cmbAssetName.Items.Add(record.Category.Trim());
            }

            //初始化“所在部门”下拉框
            foreach (Dict_Dept record in AssetsHelper.GetDepartments())
            {
                cmbDept.Items.Add(record.Dept.Trim());
            }

            //初始化“获取方式”下拉框
            foreach (Dict_GetType record in AssetsHelper.GetGetTypes())
            {
                cmbGetType.Items.Add(record.GetType.Trim());
            }

            //初始化“计量信息”下拉框
            foreach (Dict_MeasureInfo record in AssetsHelper.GetMeasureInfos())
            {
                cmbMeasureInfo.Items.Add(record.MeasureInfo.Trim());
            }

            //初始化“资产用途”下拉框
            foreach (Dict_UseFor record in AssetsHelper.GetUseFors())
            {
                cmbUseFor.Items.Add(record.UseFor.Trim());
            }
        }

        //显示资产详情
        private void InitFrmByDetails(string sCode, string sDataSource)
        {
            if (string.IsNullOrEmpty(sCode))
            {
                tbAssetCode.Text = "";
                tbAssetTag.Text = "";
                tbAssetSpec.Text = "";
                tbCodeL.Text = "";
                tbCodeM.Text = "";
                tbInBillCode.Text = "";
                tbGBCode.Text = "";
                tbTaskCode.Text = "";
                tbPurchaseFrom.Text = "";
                tbProductionFirm.Text = "";
                tbLocation.Text = "";
                tbCertificateNo.Text = "";
                tbInCharge.Text = "";

                nudOriCost.Value = 0;

                cmbAssetName.SelectedIndex = -1;
                cmbUseFor.SelectedIndex = -1;
                cmbMeasureInfo.SelectedIndex = -1;
                cmbGetType.SelectedIndex = -1;
                cmbDept.SelectedIndex = -1;

                dtpStartUseDate.Text = "";
                dtpRepairDate.Text = "";
                return;
            }

            switch (sDataSource)
            {
                case "base":
                    #region For Tbl_BaseAssets
                    Tbl_BaseAssets baserecord = AssetsHelper.GetBaseAssetByCode(sCode);
                    if (baserecord == null)
                    {
                        tbAssetCode.Text = "";
                        tbAssetTag.Text = "";
                        tbAssetSpec.Text = "";
                        tbCodeL.Text = "";
                        tbCodeM.Text = "";
                        tbInBillCode.Text = "";
                        tbGBCode.Text = "";
                        tbTaskCode.Text = "";
                        tbPurchaseFrom.Text = "";
                        tbProductionFirm.Text = "";
                        tbLocation.Text = "";
                        tbCertificateNo.Text = "";
                        tbInCharge.Text = "";

                        nudOriCost.Value = 0;

                        cmbAssetName.SelectedIndex = -1;
                        cmbUseFor.SelectedIndex = -1;
                        cmbMeasureInfo.SelectedIndex = -1;
                        cmbGetType.SelectedIndex = -1;
                        cmbDept.SelectedIndex = -1;

                        dtpStartUseDate.Text = "";
                        dtpRepairDate.Text = "";
                    }
                    else
                    {
                        tbAssetCode.Text = sCode;
                        tbAssetTag.Text = baserecord.AssetTagCode.Trim();
                        tbAssetSpec.Text = baserecord.AssetSpec.Trim();
                        tbCodeL.Text = baserecord.TypeCodeL.Trim();
                        tbCodeM.Text = baserecord.TypeCodeM.Trim();
                        tbInBillCode.Text = baserecord.InBillCode.Trim();
                        tbGBCode.Text = baserecord.GBCode.Trim();
                        tbTaskCode.Text = baserecord.TaskCode.Trim();
                        tbPurchaseFrom.Text = baserecord.PurchaseFrom.Trim();
                        tbProductionFirm.Text = baserecord.ProductionFirm.Trim();
                        tbLocation.Text = baserecord.Location.Trim();
                        tbCertificateNo.Text = baserecord.CertificateNo.Trim();
                        tbInCharge.Text = baserecord.InCharge.Trim();

                        nudOriCost.Value = baserecord.OriCost;

                        cmbAssetName.SelectedIndex = cmbAssetName.Items.IndexOf(baserecord.AssetName.Trim());
                        cmbUseFor.SelectedIndex = cmbUseFor.Items.IndexOf(baserecord.UseFor.Trim());
                        cmbMeasureInfo.SelectedIndex = cmbMeasureInfo.Items.IndexOf(baserecord.MeasureInfo.Trim());
                        cmbGetType.SelectedIndex = cmbGetType.Items.IndexOf(baserecord.GetType.Trim());
                        cmbDept.SelectedIndex = cmbDept.Items.IndexOf(baserecord.Dept.Trim());

                        dtpStartUseDate.Value = DataHelper.GetStringToDate(baserecord.StartUseDate.Trim());
                        dtpRepairDate.Value = DataHelper.GetStringToDate(baserecord.RepairDate.Trim());
                    }                    
                    break;
                    #endregion
                case "self":
                    #region For Tbl_SelfAssets
                    Tbl_SelfAssets selfrecord = AssetsHelper.GetSelfAssetByCode(sCode);
                    if (selfrecord == null)
                    {
                        tbAssetCode.Text = "";
                        tbAssetTag.Text = "";
                        tbAssetSpec.Text = "";
                        tbCodeL.Text = "";
                        tbCodeM.Text = "";
                        tbInBillCode.Text = "";
                        tbGBCode.Text = "";
                        tbTaskCode.Text = "";
                        tbPurchaseFrom.Text = "";
                        tbProductionFirm.Text = "";
                        tbLocation.Text = "";
                        tbCertificateNo.Text = "";
                        tbInCharge.Text = "";

                        nudOriCost.Value = 0;

                        cmbAssetName.SelectedIndex = -1;
                        cmbUseFor.SelectedIndex = -1;
                        cmbMeasureInfo.SelectedIndex = -1;
                        cmbGetType.SelectedIndex = -1;
                        cmbDept.SelectedIndex = -1;

                        dtpStartUseDate.Text = "";
                        dtpRepairDate.Text = "";
                    }
                    else
                    {
                        tbAssetCode.Text = sCode;
                        tbAssetTag.Text = selfrecord.AssetTagCode.Trim();
                        tbAssetSpec.Text = selfrecord.AssetSpec.Trim();
                        tbCodeL.Text = selfrecord.TypeCodeL.Trim();
                        tbCodeM.Text = selfrecord.TypeCodeM.Trim();
                        tbInBillCode.Text = selfrecord.InBillCode.Trim();
                        tbGBCode.Text = selfrecord.GBCode.Trim();
                        tbTaskCode.Text = selfrecord.TaskCode.Trim();
                        tbPurchaseFrom.Text = selfrecord.PurchaseFrom.Trim();
                        tbProductionFirm.Text = selfrecord.ProductionFirm.Trim();
                        tbLocation.Text = selfrecord.Location.Trim();
                        tbCertificateNo.Text = selfrecord.CertificateNo.Trim();
                        tbInCharge.Text = selfrecord.InCharge.Trim();

                        nudOriCost.Value = selfrecord.OriCost;

                        cmbAssetName.SelectedIndex = cmbAssetName.Items.IndexOf(selfrecord.AssetName.Trim());
                        cmbUseFor.SelectedIndex = cmbUseFor.Items.IndexOf(selfrecord.UseFor.Trim());
                        cmbMeasureInfo.SelectedIndex = cmbMeasureInfo.Items.IndexOf(selfrecord.MeasureInfo.Trim());
                        cmbGetType.SelectedIndex = cmbGetType.Items.IndexOf(selfrecord.GetType.Trim());
                        cmbDept.SelectedIndex = cmbDept.Items.IndexOf(selfrecord.Dept.Trim());

                        dtpStartUseDate.Value = DataHelper.GetStringToDate(selfrecord.StartUseDate.Trim());
                        dtpRepairDate.Value = DataHelper.GetStringToDate(selfrecord.RepairDate.Trim());
                    }
                    break;
                    #endregion
                case "measure":
                    #region For Tbl_MeasureAssets
                    Tbl_MeasureAssets measurerecord = AssetsHelper.GetMeasureAssetByCode(sCode);
                    if (measurerecord == null)
                    {
                        tbAssetCode.Text = "";
                        tbAssetTag.Text = "";
                        tbAssetSpec.Text = "";
                        tbCodeL.Text = "";
                        tbCodeM.Text = "";
                        tbInBillCode.Text = "";
                        tbGBCode.Text = "";
                        tbTaskCode.Text = "";
                        tbPurchaseFrom.Text = "";
                        tbProductionFirm.Text = "";
                        tbLocation.Text = "";
                        tbCertificateNo.Text = "";
                        tbInCharge.Text = "";

                        nudOriCost.Value = 0;

                        cmbAssetName.SelectedIndex = -1;
                        cmbUseFor.SelectedIndex = -1;
                        cmbMeasureInfo.SelectedIndex = -1;
                        cmbGetType.SelectedIndex = -1;
                        cmbDept.SelectedIndex = -1;

                        dtpStartUseDate.Text = "";
                        dtpRepairDate.Text = "";
                    }
                    else
                    {
                        tbAssetCode.Text = sCode;
                        tbAssetTag.Text = measurerecord.AssetTagCode.Trim();
                        tbAssetSpec.Text = measurerecord.AssetSpec.Trim();
                        tbCodeL.Text = measurerecord.TypeCodeL.Trim();
                        tbCodeM.Text = measurerecord.TypeCodeM.Trim();
                        tbInBillCode.Text = measurerecord.InBillCode.Trim();
                        tbGBCode.Text = measurerecord.GBCode.Trim();
                        tbTaskCode.Text = measurerecord.TaskCode.Trim();
                        tbPurchaseFrom.Text = measurerecord.PurchaseFrom.Trim();
                        tbProductionFirm.Text = measurerecord.ProductionFirm.Trim();
                        tbLocation.Text = measurerecord.Location.Trim();
                        tbCertificateNo.Text = measurerecord.CertificateNo.Trim();
                        tbInCharge.Text = measurerecord.InCharge.Trim();

                        nudOriCost.Value = measurerecord.OriCost;

                        cmbAssetName.SelectedIndex = cmbAssetName.Items.IndexOf(measurerecord.AssetName.Trim());
                        cmbUseFor.SelectedIndex = cmbUseFor.Items.IndexOf(measurerecord.UseFor.Trim());
                        cmbMeasureInfo.SelectedIndex = cmbMeasureInfo.Items.IndexOf(measurerecord.MeasureInfo.Trim());
                        cmbGetType.SelectedIndex = cmbGetType.Items.IndexOf(measurerecord.GetType.Trim());
                        cmbDept.SelectedIndex = cmbDept.Items.IndexOf(measurerecord.Dept.Trim());

                        dtpStartUseDate.Value = DataHelper.GetStringToDate(measurerecord.StartUseDate.Trim());
                        dtpRepairDate.Value = DataHelper.GetStringToDate(measurerecord.RepairDate.Trim());
                    }
                    break;
                    #endregion
                default:
                    break;
            }
        }

        //添加资产
        private void AddAsset()
        {
            if(string.IsNullOrWhiteSpace(tbAssetCode.Text.Trim()))
            {
                MessageBox.Show("请输入资产编号！");
                tbAssetCode.BackColor = Color.Yellow;
                return;
            }
            if (string.IsNullOrWhiteSpace(tbAssetTag.Text.Trim()))
            {
                MessageBox.Show("请输入标签号！");
                tbAssetTag.BackColor = Color.Yellow;
                return;
            }
            if (cmbAssetName.SelectedIndex == -1)
            {
                MessageBox.Show("请选择资产名称！");
                cmbAssetName.BackColor = Color.Yellow;
                return;
            }
            if (cmbDept.SelectedIndex == -1)
            {
                MessageBox.Show("请选择资产所在位置！");
                cmbDept.BackColor = Color.Yellow;
                return;
            }
            if (cmbGetType.SelectedIndex == -1)
            {
                MessageBox.Show("请选择资产的获取方式！");
                cmbGetType.BackColor = Color.Yellow;
                return;
            }
            if (cmbMeasureInfo.SelectedIndex == -1)
            {
                MessageBox.Show("请选择资产的计量信息！");
                cmbMeasureInfo.BackColor = Color.Yellow;
                return;
            }
            if (cmbUseFor.SelectedIndex == -1)
            {
                MessageBox.Show("请选择资产用途！");
                cmbUseFor.BackColor = Color.Yellow;
                return;
            }

            OurFasDBEntities entity = new OurFasDBEntities();

            //根据来源分别添加设备记录
            if (m_sDataSource == "base")
            {
                Tbl_BaseAssets record = new Tbl_BaseAssets();
                record.AssetCode = GetDataFromTextbox(tbAssetCode);
                record.AssetTagCode = GetDataFromTextbox(tbAssetTag);
                record.AssetSpec = GetDataFromTextbox(tbAssetSpec);
                record.TypeCodeL = GetDataFromTextbox(tbCodeL);
                record.TypeCodeM = GetDataFromTextbox(tbCodeM);
                record.InBillCode = GetDataFromTextbox(tbInBillCode);
                record.GBCode = GetDataFromTextbox(tbGBCode);
                record.TaskCode = GetDataFromTextbox(tbTaskCode);
                record.PurchaseFrom = GetDataFromTextbox(tbProductionFirm);
                record.ProductionFirm = GetDataFromTextbox(tbProductionFirm);
                record.InCharge = GetDataFromTextbox(tbInCharge);
                record.Location = GetDataFromTextbox(tbLocation);
                record.CertificateNo = GetDataFromTextbox(tbCertificateNo);

                record.AssetName = cmbAssetName.SelectedItem.ToString().Trim();
                record.UseFor = cmbUseFor.SelectedItem.ToString().Trim();
                record.MeasureInfo = cmbMeasureInfo.SelectedItem.ToString().Trim();
                record.GetType = cmbGetType.SelectedItem.ToString().Trim();
                record.Dept = cmbDept.SelectedItem.ToString().Trim();

                record.OriCost = nudOriCost.Value;

                record.StartUseDate = DataHelper.GetDateToString(dtpStartUseDate.Value);
                record.RepairDate = DataHelper.GetDateToString(dtpRepairDate.Value);

                entity.Tbl_BaseAssets.AddObject(record);
            }
            else if (m_sDataSource == "self")
            {
                Tbl_SelfAssets record = new Tbl_SelfAssets();
                record.AssetCode = GetDataFromTextbox(tbAssetCode);
                record.AssetTagCode = GetDataFromTextbox(tbAssetTag);
                record.AssetSpec = GetDataFromTextbox(tbAssetSpec);
                record.TypeCodeL = GetDataFromTextbox(tbCodeL);
                record.TypeCodeM = GetDataFromTextbox(tbCodeM);
                record.InBillCode = GetDataFromTextbox(tbInBillCode);
                record.GBCode = GetDataFromTextbox(tbGBCode);
                record.TaskCode = GetDataFromTextbox(tbTaskCode);
                record.PurchaseFrom = GetDataFromTextbox(tbProductionFirm);
                record.ProductionFirm = GetDataFromTextbox(tbProductionFirm);
                record.InCharge = GetDataFromTextbox(tbInCharge);
                record.Location = GetDataFromTextbox(tbLocation);
                record.CertificateNo = GetDataFromTextbox(tbCertificateNo);

                record.AssetName = cmbAssetName.SelectedItem.ToString().Trim();
                record.UseFor = cmbUseFor.SelectedItem.ToString().Trim();
                record.MeasureInfo = cmbMeasureInfo.SelectedItem.ToString().Trim();
                record.GetType = cmbGetType.SelectedItem.ToString().Trim();
                record.Dept = cmbDept.SelectedItem.ToString().Trim();

                record.OriCost = nudOriCost.Value;

                record.StartUseDate = DataHelper.GetDateToString(dtpStartUseDate.Value);
                record.RepairDate = DataHelper.GetDateToString(dtpRepairDate.Value);
                
                entity.Tbl_SelfAssets.AddObject(record);
            }
            else if (m_sDataSource == "measure")
            {
                Tbl_MeasureAssets record = new Tbl_MeasureAssets();
                record.AssetCode = GetDataFromTextbox(tbAssetCode);
                record.AssetTagCode = GetDataFromTextbox(tbAssetTag);
                record.AssetSpec = GetDataFromTextbox(tbAssetSpec);
                record.TypeCodeL = GetDataFromTextbox(tbCodeL);
                record.TypeCodeM = GetDataFromTextbox(tbCodeM);
                record.InBillCode = GetDataFromTextbox(tbInBillCode);
                record.GBCode = GetDataFromTextbox(tbGBCode);
                record.TaskCode = GetDataFromTextbox(tbTaskCode);
                record.PurchaseFrom = GetDataFromTextbox(tbProductionFirm);
                record.ProductionFirm = GetDataFromTextbox(tbProductionFirm);
                record.InCharge = GetDataFromTextbox(tbInCharge);
                record.Location = GetDataFromTextbox(tbLocation);
                record.CertificateNo = GetDataFromTextbox(tbCertificateNo);

                record.AssetName = cmbAssetName.SelectedItem.ToString().Trim();
                record.UseFor = cmbUseFor.SelectedItem.ToString().Trim();
                record.MeasureInfo = cmbMeasureInfo.SelectedItem.ToString().Trim();
                record.GetType = cmbGetType.SelectedItem.ToString().Trim();
                record.Dept = cmbDept.SelectedItem.ToString().Trim();

                record.OriCost = nudOriCost.Value;

                record.StartUseDate = DataHelper.GetDateToString(dtpStartUseDate.Value);
                record.RepairDate = DataHelper.GetDateToString(dtpRepairDate.Value);
                
                entity.Tbl_MeasureAssets.AddObject(record);
            }

            try
            {
                entity.SaveChanges();
                MessageBox.Show("已添加资产编号为 " + tbAssetCode.Text.Trim() + " 的资产记录！");
                this.Close();
            }
            catch
            {
                MessageBox.Show("添加资产出错，请重试或联系管理员！");
                return;
            }

        }

        //修改指定资产
        private void EditAsset(string code)
        {
            OurFasDBEntities entity = new OurFasDBEntities();

            if(cmbAssetName.SelectedIndex == -1)
            {
                MessageBox.Show("请选择资产名称！");
                cmbAssetName.BackColor = Color.Yellow;
                return;
            }
            if(cmbDept.SelectedIndex == -1)
            {
                MessageBox.Show("请选择资产所在位置！");
                cmbDept.BackColor = Color.Yellow;
                return;
            }
            if(cmbGetType.SelectedIndex ==-1)
            {
                MessageBox.Show("请选择资产的获取方式！");
                cmbGetType.BackColor = Color.Yellow;
                return;
            }
            if(cmbMeasureInfo.SelectedIndex == -1)
            {
                MessageBox.Show("请选择资产的计量信息！");
                cmbMeasureInfo.BackColor = Color.Yellow;
                return;
            }
            if(cmbUseFor.SelectedIndex == -1)
            {
                MessageBox.Show("请选择资产用途！");
                cmbUseFor.BackColor =Color.Yellow;
                return;
            }
            if (m_sDataSource == "base")
            {
                Tbl_BaseAssets record = entity.Tbl_BaseAssets.Where(c => c.AssetCode.Trim() == code).FirstOrDefault();
                if (record != null)
                {
                    record.AssetTagCode = GetDataFromTextbox(tbAssetTag);
                    record.AssetSpec = GetDataFromTextbox(tbAssetSpec);
                    record.TypeCodeL = GetDataFromTextbox(tbCodeL);
                    record.TypeCodeM = GetDataFromTextbox(tbCodeM);
                    record.InBillCode = GetDataFromTextbox(tbInBillCode);
                    record.GBCode = GetDataFromTextbox(tbGBCode);
                    record.TaskCode = GetDataFromTextbox(tbTaskCode);
                    record.PurchaseFrom = GetDataFromTextbox(tbPurchaseFrom);
                    record.ProductionFirm = GetDataFromTextbox(tbProductionFirm);
                    record.Location = GetDataFromTextbox(tbLocation);
                    record.CertificateNo = GetDataFromTextbox(tbCertificateNo);
                    record.InCharge = GetDataFromTextbox(tbInCharge);

                    record.AssetName = cmbAssetName.SelectedItem.ToString().Trim();
                    record.UseFor = cmbUseFor.SelectedItem.ToString().Trim();
                    record.MeasureInfo = cmbMeasureInfo.SelectedItem.ToString().Trim();
                    record.GetType = cmbGetType.SelectedItem.ToString().Trim();
                    record.Dept = cmbDept.SelectedItem.ToString().Trim();

                    record.OriCost = nudOriCost.Value;

                    record.StartUseDate = DataHelper.GetDateToString(dtpStartUseDate.Value);
                    record.RepairDate = DataHelper.GetDateToString(dtpRepairDate.Value);
                }
            }
            else if (m_sDataSource == "self")
            {
                Tbl_SelfAssets record = entity.Tbl_SelfAssets.Where(c => c.AssetCode.Trim() == code).FirstOrDefault();
                if (record != null)
                {
                    record.AssetTagCode = GetDataFromTextbox(tbAssetTag);
                    record.AssetSpec = GetDataFromTextbox(tbAssetSpec);
                    record.TypeCodeL = GetDataFromTextbox(tbCodeL);
                    record.TypeCodeM = GetDataFromTextbox(tbCodeM);
                    record.InBillCode = GetDataFromTextbox(tbInBillCode);
                    record.GBCode = GetDataFromTextbox(tbGBCode);
                    record.TaskCode = GetDataFromTextbox(tbTaskCode);
                    record.PurchaseFrom = GetDataFromTextbox(tbPurchaseFrom);
                    record.ProductionFirm = GetDataFromTextbox(tbProductionFirm);
                    record.Location = GetDataFromTextbox(tbLocation);
                    record.CertificateNo = GetDataFromTextbox(tbCertificateNo);
                    record.InCharge = GetDataFromTextbox(tbInCharge);

                    record.AssetName = cmbAssetName.SelectedItem.ToString().Trim();
                    record.UseFor = cmbUseFor.SelectedItem.ToString().Trim();
                    record.MeasureInfo = cmbMeasureInfo.SelectedItem.ToString().Trim();
                    record.GetType = cmbGetType.SelectedItem.ToString().Trim();
                    record.Dept = cmbDept.SelectedItem.ToString().Trim();

                    record.OriCost = nudOriCost.Value;

                    record.StartUseDate = DataHelper.GetDateToString(dtpStartUseDate.Value);
                    record.RepairDate = DataHelper.GetDateToString(dtpRepairDate.Value);
                }
            }
            else if (m_sDataSource == "measure")
            {
                Tbl_MeasureAssets record = entity.Tbl_MeasureAssets.Where(c => c.AssetCode.Trim() == code).FirstOrDefault();
                if (record != null)
                {
                    record.AssetTagCode = GetDataFromTextbox(tbAssetTag);
                    record.AssetSpec = GetDataFromTextbox(tbAssetSpec);
                    record.TypeCodeL = GetDataFromTextbox(tbCodeL);
                    record.TypeCodeM = GetDataFromTextbox(tbCodeM);
                    record.InBillCode = GetDataFromTextbox(tbInBillCode);
                    record.GBCode = GetDataFromTextbox(tbGBCode);
                    record.TaskCode = GetDataFromTextbox(tbTaskCode);
                    record.PurchaseFrom = GetDataFromTextbox(tbPurchaseFrom);
                    record.ProductionFirm = GetDataFromTextbox(tbProductionFirm);
                    record.Location = GetDataFromTextbox(tbLocation);
                    record.CertificateNo = GetDataFromTextbox(tbCertificateNo);
                    record.InCharge = GetDataFromTextbox(tbInCharge);

                    record.AssetName = cmbAssetName.SelectedItem.ToString().Trim();
                    record.UseFor = cmbUseFor.SelectedItem.ToString().Trim();
                    record.MeasureInfo = cmbMeasureInfo.SelectedItem.ToString().Trim();
                    record.GetType = cmbGetType.SelectedItem.ToString().Trim();
                    record.Dept = cmbDept.SelectedItem.ToString().Trim();

                    record.OriCost = nudOriCost.Value;

                    record.StartUseDate = DataHelper.GetDateToString(dtpStartUseDate.Value);
                    record.RepairDate = DataHelper.GetDateToString(dtpRepairDate.Value);
                }
            }
            try
            {
                entity.SaveChanges();
                MessageBox.Show("已修改资产编号为 " + code + " 的资产记录！");
                this.Close();
            }
            catch
            {
                MessageBox.Show("修改资产出错，请重试或联系管理员！");
                return;
            }
        }

        //删除制定资产
        private void DeleteAsset(string code)
        {
            OurFasDBEntities entity = new OurFasDBEntities();
            if (m_sDataSource == "base")
            {
                Tbl_BaseAssets record = entity.Tbl_BaseAssets.Where(c => c.AssetCode.Trim() == code).FirstOrDefault();
                if (record != null)
                {
                    entity.Tbl_BaseAssets.DeleteObject(record);
                }
            }
            else if (m_sDataSource == "self")
            {
                Tbl_SelfAssets record = entity.Tbl_SelfAssets.Where(c => c.AssetCode.Trim() == code).FirstOrDefault();
                if (record != null)
                {
                    entity.Tbl_SelfAssets.DeleteObject(record);
                }
            }
            else if (m_sDataSource == "measure")
            {
                Tbl_MeasureAssets record = entity.Tbl_MeasureAssets.Where(c => c.AssetCode.Trim() == code).FirstOrDefault();
                if (record != null)
                {
                    entity.Tbl_MeasureAssets.DeleteObject(record);
                }
            }
            try
            {
                entity.SaveChanges();
                MessageBox.Show("已删除资产编号为 " + code + " 的资产记录！");
                this.Close();
            }
            catch
            {
                MessageBox.Show("删除数据出错，请重试或联系管理员！");
                return;
            }
        }

        //从文本框控件中读取信息
        private string GetDataFromTextbox(TextBox tb)
        {
            string result = "";
            if (tb == null)
            {
                return result;
            }
            else if(tb.Text.Trim() == "")
            {
                return result;
            }
            else
            {
                result = tb.Text.Trim();
                return result;
            }
        }
        #endregion
    }
}
